package com.ihome.android.market2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihome.android.EAApp;
import com.ihome.android.academy.AppMgrActivity;
import com.ihome.android.dl.util.DLFileInfo;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.image.CubeImageView;
import com.ihome.android.image.ImageLoader;
import com.ihome.android.academy.R;
import com.ihome.android.market2.asynctask.InstallAppTask;
import com.ihome.android.market2.frag.DownloadingAppFragment.DownloadingAppAdapter;
import com.ihome.android.util.BgInstallApk;
import com.ihome.android.util.log.LogUtil;

@SuppressLint("NewApi")
public class AppDownloadingItem implements OnClickListener {
	private final String			TAG			= AppDownloadingItem.class.getSimpleName();
	private RelativeLayout			mRelativeLayout;

	private RelativeLayout			fl_content;
	private FrameLayout				fl_left;

	private CubeImageView			item_icon;
	private TextView				item_appname;
	private TextView				item_sec_info;
	private TextView				item_size;

	//删除按钮
	private TextView				tv_del;
	private FrameLayout				fl_right;
	//下载更新进度条
	private ProgressBar				progress_bar;

	private Context					context;
	private ImageLoader				imageLoader;

	private DownloadingAppAdapter	adapter;
	private DLFileInfo				mDLFileInfo;
	public int						position;

	private boolean					isMove2Del	= false;
	public static boolean			beFocusable	= false;
	private Drawable[]				drawables	= null;

	public AppDownloadingItem(Context context, ImageLoader imageLoader) {
		this.context = context;
		this.imageLoader = imageLoader;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.app_downloading_item, null);
		fl_content = (RelativeLayout) mRelativeLayout.findViewById(R.id.fl_content);
		fl_left = (FrameLayout) mRelativeLayout.findViewById(R.id.fl_left);

		item_icon = (CubeImageView) mRelativeLayout.findViewById(R.id.item_icon);
		item_appname = (TextView) mRelativeLayout.findViewById(R.id.item_appname);
		item_sec_info = (TextView) mRelativeLayout.findViewById(R.id.item_sec_info);
		item_size = (TextView) mRelativeLayout.findViewById(R.id.item_size);
		progress_bar = (ProgressBar) mRelativeLayout.findViewById(R.id.progress_bar);

		tv_del = (TextView) mRelativeLayout.findViewById(R.id.tv_del);
		fl_right = (FrameLayout) mRelativeLayout.findViewById(R.id.fl_right);

		fl_content.setOnClickListener(this);
		tv_del.setOnClickListener(this);

		drawables = tv_del.getCompoundDrawables();

		fl_content.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				LogUtil.w(TAG, "onFocusChange() --> fl_content hasFocus = " + hasFocus + "  position = " + position + "  isMove2Del = " + isMove2Del);
				if (beFocusable == false) {
					return;
				}
				if (hasFocus) {
					//					fl_left.setVisibility(View.VISIBLE);
					isMove2Del = false;
					adapter.setFocusedPosition(position);
					fl_left.setVisibility(View.VISIBLE);
					restoreTextview();
					hasFocus();
				}
				else {
					fl_left.setVisibility(View.GONE);

					clearTextview();
					fl_right.setVisibility(View.GONE);
					mRelativeLayout.setBackgroundResource(R.drawable.list_bg);
					fl_content.setBackgroundResource(R.drawable.list_bg_left_blank);
					tv_del.setBackgroundResource(R.drawable.list_bg_right_blank);

					//					recovered();
				}
				checkMainViewFocus();
			}
		});

		tv_del.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				LogUtil.i(TAG, "onFocusChange() --> tv_del hasFocus = " + hasFocus + "  position = " + position);
				if (beFocusable == false) {
					return;
				}
				if (hasFocus) {
					fl_right.setVisibility(View.VISIBLE);

					restoreTextview();

					hasFocus();
				}
				else {
					fl_right.setVisibility(View.GONE);
					recovered();
				}
				checkMainViewFocus();
			}
		});

	}

	/**
	 * 初始化View，根据AppInfo填充View
	 * 
	 * @param adapter
	 * @param fileInfo
	 * @param position
	 */
	public void initView(DownloadingAppAdapter adapter, DLFileInfo fileInfo, int position) {
		this.adapter = adapter;
		this.mDLFileInfo = fileInfo;
		this.position = position;
		if (fileInfo != null) {

			if (fileInfo != null) {
				//LogUtil.d(TAG, " --> AppInfo = " + appInfo.toString());
				item_appname.setText(fileInfo.getFileName().split(".apk")[0]);

				if (!TextUtils.isEmpty(fileInfo.getIconUrl())) {
					item_icon.loadImage(imageLoader, fileInfo.getIconUrl());
				}

				if (fileInfo.getStatus() == DLFileInfo.STATUS_DOWNLOADING) {
					item_size.setText(fileInfo.getProgress() + "%");
					progress_bar.setVisibility(View.VISIBLE);
					progress_bar.setProgress(fileInfo.getProgress());
					fl_left.setBackgroundResource(R.drawable.list_highlight_left_ztxz);
				}
				else if (fileInfo.getStatus() == DLFileInfo.STATUS_DOWNLOAD_END) {
					item_size.setText("100%");
					progress_bar.setVisibility(View.VISIBLE);
					progress_bar.setProgress(100);
					fl_left.setBackgroundResource(R.drawable.list_highlight_left_ljaz);
				}
				else {
					item_size.setText(context.getString(R.string.pause));
					progress_bar.setVisibility(View.VISIBLE);
					progress_bar.setSecondaryProgress(mDLFileInfo.getProgress());
					fl_left.setBackgroundResource(R.drawable.list_highlight_left_jxxz);
				}

			}
		}
	}

	private void checkMainViewFocus() {
		if (!mRelativeLayout.hasFocus()) {
			LogUtil.w(TAG, "-----recovered");
			recovered();
		}
	}

	public void recovered() {
		//LogUtil.w(TAG, "recovered() -->  position = " + position);
		fl_right.setVisibility(View.GONE);
		fl_left.setVisibility(View.GONE);
		tv_del.setVisibility(View.INVISIBLE);

		mRelativeLayout.setBackgroundResource(R.drawable.list_bg);
		fl_content.setBackgroundResource(R.drawable.list_bg_left_blank);
		tv_del.setBackgroundResource(R.drawable.list_bg_right_blank);
	}

	public void hasFocus() {
		tv_del.setVisibility(View.VISIBLE);
		mRelativeLayout.setBackgroundResource(R.drawable.list_bg_blank);
		fl_content.setBackgroundResource(R.drawable.list_bg_left);
		tv_del.setBackgroundResource(R.drawable.list_bg_right);
	}

	public void onFocused() {
		LogUtil.e(TAG, "onFocused");
		/*		tv_del.setVisibility(View.VISIBLE);
				mRelativeLayout.setBackgroundResource(R.drawable.list_bg_blank);
				fl_content.setBackgroundResource(R.drawable.list_bg_left);
				tv_del.setBackgroundResource(R.drawable.list_bg_right);

				if (tv_del.hasFocus() == false) {
					LogUtil.e(TAG, "onFocused > fl_content.requestFocus()");
					fl_left.setVisibility(View.VISIBLE);
					fl_content.requestFocus();
				}*/
	}

	private void clearTextview() {
		tv_del.setText("");
		tv_del.getCompoundDrawables();
		tv_del.setCompoundDrawables(null, null, null, null);
	}

	private void restoreTextview() {
		tv_del.setText(R.string.cancel);
		tv_del.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
	}

	public void requestFocus() {
		adapter.setFocusedPosition(position);
		hasFocus();
		fl_left.setVisibility(View.VISIBLE);
		fl_content.requestFocus();
	}

	public void setClickListener(OnClickListener onClickListener) {
		mRelativeLayout.setOnClickListener(onClickListener);
	}

	public View getView() {
		return mRelativeLayout;
	}

	public void setLayoutParams(RelativeLayout.LayoutParams params) {
		mRelativeLayout.setLayoutParams(params);
	}

	public void setTag(Object tag) {
		mRelativeLayout.setTag(tag);
	}

	public void setOnKeyListener(OnKeyListener onkeylistener) {
		mRelativeLayout.setOnKeyListener(onkeylistener);
		fl_content.setOnKeyListener(onkeylistener);
		tv_del.setOnKeyListener(onkeylistener);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fl_content:
			LogUtil.w(TAG, "onClick() --> fl_content  position = " + position);
			if (mDLFileInfo.getStatus() == DLFileInfo.STATUS_DOWNLOAD_END) {
				fl_left.setBackgroundResource(R.drawable.list_highlight_left_ljaz);
				String filePath = mDLFileInfo.getFilePath() + mDLFileInfo.getFileName();
				if (EAApp.isSystemApp) {
					Bundle b = new Bundle();
					b.putString(HttpKeys.KEY_APKPATH, filePath);
					new InstallAppTask(context).execute(b);
				}
				else {
					BgInstallApk.doInstallation(context, filePath);
				}
			}
			else if (mDLFileInfo.getStatus() == DLFileInfo.STATUS_DOWNLOADING) {
				mDLFileInfo.setStatus(DLFileInfo.STATUS_DOWNLOAD_STOP);
				adapter.pauseDLingApp(mDLFileInfo);
				fl_left.setBackgroundResource(R.drawable.list_highlight_left_jxxz);
			}
			else {
				mDLFileInfo.setStatus(DLFileInfo.STATUS_DOWNLOADING);
				adapter.startDLingApp(mDLFileInfo);
				fl_left.setBackgroundResource(R.drawable.list_highlight_left_ztxz);
			}
			break;
		case R.id.tv_del:
			LogUtil.v(TAG, "onClick() --> tv_del_or_install  position = " + position);
			adapter.delDLingApp(mDLFileInfo);
			break;
		default:
			break;
		}
	}
}
